<!-- +----------------------------------------------------------------------
| 麦沃德科技赋能开发者，助力中小企业发展 
+----------------------------------------------------------------------
| Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
+----------------------------------------------------------------------
| 沃德代驾系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
+----------------------------------------------------------------------
| Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
+----------------------------------------------------------------------
| 优惠卡券 开发者: 麦沃德科技-半夏
+---------------------------------------------------------------------- -->

<template>
	<view class="container" :style="{'--theme-color': themeColor}" v-if="loadEnd">
		<!-- 顶部导航 -->
		<view class="container-screen flex">
			<view class="screen-item" @click="changeScreen(1)">
				<view class="text" :class="{active: selectScreen == 1}">我的卡券</view>
			</view>
			<view class="screen-item" @click="changeScreen(2)">
				<view class="text" :class="{active: selectScreen == 2}">领券中心</view>
			</view>
		</view>
		<!-- 优惠券列表 -->
		<view class="container-main">
			<coupon-list :show-data="couponList" :show-type="selectScreen" :loading="couponLoading" @changeReceive="changeReceiveStatus" v-if="couponList.length"></coupon-list>
			<view class="main-empty" v-else>
				<image class="empty-icon" src="/static/passenger/coupon_empty.png" mode="widthFix"></image>
				<view class="empty-text">暂无优惠券~</view>
			</view>
		</view>
	</view>
</template>

<script>
	import couponList from "@/pages/passenger/component/coupon/list.vue"
	import { mapState } from "vuex"
	export default {
		components: {
			couponList,
		},
		data() {
			return {
				// 加载完成
				loadEnd: false,
				// 已选分类
				selectScreen: 1,
				// 分类查询参数
				page: 1,
				limit: 20,
				hasMore: false,
				// 优惠券列表
				couponList: [],
				// 优惠券列表是否加载
				couponLoading: false,
			}
		},
		computed: {
			...mapState({
				themeColor: state => state.app.themeColor,
				shareTitle: state => state.shareTitle,
				shareImage: state => state.shareImage,
			})
		},
		onLoad() {
			uni.showLoading({
				title: "加载中"
			})
			this.getCouponList(() => {
				uni.hideLoading()
				this.loadEnd = true
			})
		},
		onPullDownRefresh() {
			this.page = 1
			this.couponLoading = true
			this.getCouponList(() => {
				this.couponLoading = false
				uni.stopPullDownRefresh()
			})
		},
		onReachBottom() {
			if (this.hasMore) {
				this.page++
				this.getCouponList()
			}
		},
		onShareAppMessage() {
			return {
				title: this.shareTitle,
				imageUrl: this.shareImage,
			}
		},
		onShareTimeline() {
			return {
				title: this.shareTitle,
				imageUrl: this.shareImage,
			}
		},
		methods: {
			// 更改分类
			changeScreen(state) {
				this.selectScreen = state
				this.page = 1
				this.couponLoading = true
				this.getCouponList(() => {
					this.couponLoading = false
				})
			},
			// 获取优惠券列表
			getCouponList(fn) {
				var url = ""
				if (this.selectScreen == 1) {
					url = "passenger.coupon.myList"
				} else if (this.selectScreen == 2) {
					url = "passenger.coupon.center"
				}
				this.$util.request(url, {
					page: this.page,
					limit: this.limit,
				}).then(res => {
					if (fn) fn()
					if (res.code == 1) {
						let list = res.data.data
						this.hasMore = this.page < res.data.total / this.limit ? true : false
						this.couponList = this.page == 1 ? list : [...this.couponList, ...list];
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					if (fn) fn()
					console.error('获取优惠券列表 ', error)
				})
			},
			// 修改领取状态
			changeReceiveStatus(id) {
				const index = this.couponList.findIndex(item => item.id == id)
				if (index > -1) {
					this.$set(this.couponList[index], "received", 1)
				}
			},
		}
	}
</script>

<style lang="scss">
	.container {
		.container-screen {
			background: #FFF;
			position: sticky;
			top: 0;
			z-index: 99;

			.screen-item {
				width: 50%;
				display: flex;
				justify-content: center;

				.text {
					padding: 28rpx 0 20rpx;
					color: #242629;
					font-size: 28rpx;
					line-height: 40rpx;
					text-align: center;
					border-bottom: 8rpx solid transparent;

					&.active {
						color: var(--theme-color);
						border-color: var(--theme-color);
					}
				}
			}
		}

		.container-main {
			padding: 32rpx;

			.main-empty {
				padding-top: 30%;

				.empty-icon {
					width: 320rpx;
					height: 100%;
					margin: 0 auto;
				}

				.empty-text {
					color: #8D929C;
					text-align: center;
					font-size: 28rpx;
					font-weight: 600;
					line-height: 40rpx;
				}
			}
		}
	}
</style>